package club.kingyin.kyblog.core.business.service.cloud;

import club.kingyin.config.v2.imp.Config;
import club.kingyin.kyblog.core.business.dao.admin.Token;
import club.kingyin.kyblog.core.utils.environment.BlogSystemVariable;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author ：kingyin
 * @date ：创建于 2021/8/27 8:24 下午
 * @description ：
 * @modified By：
 * @version: 1.0.0
 */

@Service
public class QiniuService {

    @Autowired
    private Config config;

    private volatile Auth auth;

    // 保证安全的单例模式
    private Auth getAuth() {
        if (auth == null) {
            synchronized (this) {
                if (auth == null) {
                    String ak = config.get(BlogSystemVariable.ACCESS_KEY, String.class);
                    String sk = config.get(BlogSystemVariable.SECRET_KEY, String.class);
                    auth = Auth.create(ak,sk);
                }
                return auth;
            }
        }
        return auth;
    }

    public String getAddress() {
        return config.get(BlogSystemVariable.CLOUD_ADDRESS, String.class);
    }

    private Configuration getConfiguration() {
        Region region;
        switch (config.get(BlogSystemVariable.REGION, String.class)) {
            case BlogSystemVariable.HUA_NAN:
                region = Region.huanan();
                break;
            case BlogSystemVariable.HUA_DONG:
                region = Region.huadong();
                break;
            case BlogSystemVariable.HUA_BEI:
                region = Region.huabei();
                break;
            case BlogSystemVariable.BEI_MEI:
                region = Region.beimei();
                break;
            case BlogSystemVariable.XIN_JIA_PO_:
                region = Region.xinjiapo();
                break;
            default:
                region = Region.autoRegion();
                break;
        }
        return new Configuration(region);
    }

    public Token getUploadToken() {
        String s = getAuth().uploadToken(config.get(BlogSystemVariable.BUCKET, String.class));
        Token token = new Token();
        token.setToken(s);
        return token;
    }

    public BucketManager.FileListIterator getFiles(String prefix) {
        BucketManager bucketManager = new BucketManager(getAuth(), getConfiguration());
        return bucketManager.createFileListIterator(config.get(BlogSystemVariable.BUCKET, String.class), prefix);
    }

    public ArrayList<String> getUrlsByPrefix(String prefix) {
        ArrayList<String> imgs = new ArrayList<>();
        BucketManager.FileListIterator files = getFiles(prefix);
        while(files.hasNext()) {
            //处理获取的file list结果
            FileInfo[] items = files.next();
            for (FileInfo item : items) {
                imgs.add(getAddress()+item.key);
            }
        }
        return imgs;
    }
}
